//
//  AlbumPhotoCell.m
//  OntheRoadV4
//
//  Created by ZhangYanhua on 14/11/26.
//  Copyright (c) 2014年 EricHuang. All rights reserved.
//

#import "AlbumPhotoCell.h"
#import "AlbumViewController.h"
#import "QWImageMangerConfig.h"

@interface AlbumPhotoCell()

@end

@implementation AlbumPhotoCell

- (void)awakeFromNib
{
    // Initialization code

    self.selectionStyle = UITableViewCellSelectionStyleNone;
    
    _imageViews = [NSArray arrayWithObjects:_imageView1, _imageView2, _imageView3, _imageView4, nil];
    _selectBtns = [NSArray arrayWithObjects:_selectBtn1, _selectBtn2, _selectBtn3, _selectBtn4, nil];
    _overlayViews = [NSArray arrayWithObjects:_overlayView1, _overlayView2, _overlayView3, _overlayView4, nil];
    _videoIcons = [NSArray arrayWithObjects:_videoIcon1, _videoIcon2, _videoIcon3, _videoIcon4, nil];
    _videoShadows = [NSArray arrayWithObjects:_videoShadowImgView1, _videoShadowImgView2, _videoShadowImgView3, _videoShadowImgView4, nil];
    _videoLabels = [NSArray arrayWithObjects:_videoDurationLabel1, _videoDurationLabel2, _videoDurationLabel3, _videoDurationLabel4, nil];
    
    for (UIImageView *imgView in _imageViews) {
        [imgView addGestureRecognizer:[[UITapGestureRecognizer alloc]
                                       initWithTarget:self
                                       action:@selector(tapOnCellImageView:)]];
    }
    
    self.cameraMaskImgView.image = [QWImageMangerConfig imageNamed:@"icon_camera_96"];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

+ (CGFloat)getCellHeightIsLastCell:(BOOL)isLastCell
                   numberOfColumns:(NSInteger)cols;
{
    NSUInteger cellHeight = (SCREEN_WIDTH - (cols+1)*2) / cols + 4.0f;
    return isLastCell ? cellHeight + 4.0f : cellHeight;
}

- (void)updateCellContentWithArray:(NSArray *)assetArray selectedArray:(NSArray *)selectedArray
{
    for (UIImageView *imgView in _imageViews) {
        imgView.hidden = YES;
    }
    
    for (UIButton *selBtn in _selectBtns) {
        selBtn.hidden = YES;
    }
    
    for (UIView *view in _overlayViews) {
        view.hidden = YES;
    }
    
    for (UIImageView *view in _videoIcons) {
        view.hidden = YES;
    }
    
    for (UIView *view in _videoShadows) {
        view.hidden = YES;
    }
    
    for (UIView *view in _videoLabels) {
        view.hidden = YES;
    }
    
    self.contentView.tag += 1;
    NSInteger currentTag = self.contentView.tag;
    
    self.cameraMaskImgView.hidden = YES;
    
    for (int i = 0; i < [assetArray count]; i++) {
        ImageAsset *imgAsset = [assetArray objectAtIndex:i];
        
        UIImageView *curImgView = [self.imageViews objectAtIndex:i];
        UIButton *curBtn = [self.selectBtns objectAtIndex:i];
        UIView *curOverlay = [self.overlayViews objectAtIndex:i];
        UIImageView *videoIcon = [self.videoIcons objectAtIndex:i];
        UIImageView *videoShadow = [self.videoShadows objectAtIndex:i];
        UILabel *videoLabel = [self.videoLabels objectAtIndex:i];

        curBtn.alpha = imgAsset.isPhoto ? 1.0f : 0.0f;
        videoIcon.hidden = (imgAsset.isPhoto || !imgAsset.asset);
        videoShadow.hidden = (imgAsset.isPhoto || !imgAsset.asset);
        videoLabel.hidden = (imgAsset.isPhoto || !imgAsset.asset);

        if (!imgAsset.isPhoto) {
            videoLabel.text = imgAsset.durationString;
            videoShadow.image = [QWImageMangerConfig getStrenchImageHasCache:@"bg_photo_video_shadow"];
        }

        [curImgView setHidden:NO];
        
        if (imgAsset.asset == nil) {
            curOverlay.hidden = YES;

            [curImgView setImage:nil];
            self.cameraMaskImgView.hidden = NO;
        } else {
            NSInteger imagesPerRow = 3;
            if ([UIScreen mainScreen].bounds.size.width > 374.0f) {
                imagesPerRow = 4;
            }
            NSUInteger imageWidth = (SCREEN_WIDTH - (imagesPerRow+1)*2) / imagesPerRow;
            CGSize thumbnailSize = CGSizeMake(imageWidth * SCREEN_SCALE, imageWidth * SCREEN_SCALE);
            
            __weak AlbumPhotoCell *weakSelf = self;
            [imgAsset getThumbnailImageWithSize:thumbnailSize
                                  resultHandler:^(UIImage *result, NSDictionary *info) {
                                      if (weakSelf.contentView.tag == currentTag) {
                                          [curImgView setImage:result];
                                      }
                                  }];

            [curBtn setHidden:NO];
            
            if (!curBtn.isHidden && [selectedArray containsObject:imgAsset]) {
                curOverlay.hidden = NO;

                [curBtn setImage:[QWImageMangerConfig  imageNamed:@"icon_select_50"]
                        forState:UIControlStateNormal];
            } else {
                [curBtn setImage:[QWImageMangerConfig  imageNamed:@"icon_unselect_50"]
                        forState:UIControlStateNormal];
            }
        }
    }
}

- (void)updateCellSelectionState:(BOOL)isSelected AtIndex:(NSInteger)index
{

    UIButton *curBtn = [self.selectBtns objectAtIndex:index];
    UIView *curOverlay = [self.overlayViews objectAtIndex:index];

    if (isSelected) {
        curOverlay.hidden = NO;
        [curBtn setImage:[QWImageMangerConfig  imageNamed:@"icon_select_50"]
                forState:UIControlStateNormal];
    } else {
        curOverlay.hidden = YES;
        [curBtn setImage:[QWImageMangerConfig  imageNamed:@"icon_unselect_50"]
                forState:UIControlStateNormal];
    }
}

- (IBAction)selectButtonPressed:(UIButton *)sender {
    if ([self.delegate respondsToSelector:@selector(albumPhotoCell:didSelectAssetAtIndex:)]) {
        [self.delegate albumPhotoCell:self didSelectAssetAtIndex:[sender tag]];
    }
}

- (void)tapOnCellImageView:(UIGestureRecognizer *)recg
{
    if ([self.delegate respondsToSelector:@selector(albumPhotoCell:didSelectImageAtIndex:)]) {
        [self.delegate albumPhotoCell:self didSelectImageAtIndex:[recg.view tag]];
    }
}

@end
